namespace Votations.NSurvey.SQLServerDAL
{
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using Votations.NSurvey.Data;
    using Votations.NSurvey.IDAL;
    
    /// <summary>
    /// SQL Server DAL implementation.
    /// </summary>
    public class MultiLanguage : IMultiLanguage
    {
        /// <summary>
        /// Check if the language code is enabled for the survey
        /// and if its not returns the default enabled language code
        /// </summary>
        public string CheckSurveyLanguage(int surveyId, string languageCode)
        {
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@SurveyId", surveyId);
            commandParameters[1] = new SqlParameter("@LanguageCode", languageCode);
            object obj2 = SqlHelper.ExecuteScalar(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spMutliLanguageCheckForSurvey", commandParameters);
            if ((obj2 != null) && (obj2 != DBNull.Value))
            {
                return obj2.ToString();
            }
            return "";
        }

        /// <summary>
        /// Deletes a language from the survey
        /// </summary>
        public void DeleteSurveyLanguage(int surveyId, string languageCode,string Entity)
        {
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@SurveyId", SqlDbType.Int, 4);
            commandParameters[0].Value = surveyId;
            commandParameters[1] = new SqlParameter("@LanguageCode", SqlDbType.NVarChar, 50);
            commandParameters[1].Value = languageCode;
            commandParameters[2] = new SqlParameter("@Entity", SqlDbType.VarChar, 20);
            commandParameters[2].Value = Entity;
            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spMutliLanguageDeleteForSurvey", commandParameters);
        }

        /// <summary>
        /// Disable survey's multi language features
        /// </summary>
        /// <param name="surveyId"></param>
        public void DisableMultiLanguage(int surveyId)
        {
            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spSurveyMultiLanguageModeClearSettings", new SqlParameter[] { new SqlParameter("@SurveyId", surveyId) });
        }

        /// <summary>
        /// Returns current mode
        /// </summary>
        public MultiLanguageMode GetMultiLanguageMode(int surveyId)
        {
            object obj2 = SqlHelper.ExecuteScalar(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spSurveyMultiLanguageModeGet", new SqlParameter[] { new SqlParameter("@SurveyID", surveyId) });
            if ((obj2 != null) && (obj2 != DBNull.Value))
            {
                return (MultiLanguageMode) int.Parse(obj2.ToString());
            }
            return MultiLanguageMode.None;
        }

        /// <summary>
        /// Returns all languages that can be enabled
        /// </summary>
        public MultiLanguageData GetMultiLanguages()
        {
            MultiLanguageData dataSet = new MultiLanguageData();
            SqlHelper.FillDataset(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spMultiLanguageGetAll", dataSet, new string[] { "MultiLanguages" });
            return dataSet;
        }

        /// <summary>
        /// Returns all enabled languages
        /// </summary>
        public MultiLanguageData GetSurveyLanguages(int surveyId,string Entity)
        {
            MultiLanguageData dataSet = new MultiLanguageData();
            SqlHelper.FillDataset(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spMutliLanguageGetEnabledForSurvey", dataSet,
                new string[] { "MultiLanguages" }, new SqlParameter[] { new SqlParameter("@SurveyId", surveyId), new SqlParameter("@Entity",Entity) });
            return dataSet;
        }

        /// <summary>
        /// Updates the current multi language mode that defines how 
        /// the user selects his language
        /// </summary>
        /// <param name="surveyId"></param>
        /// <param name="languageMode"></param>
        public void UpdateMultiLanguage(int surveyId, MultiLanguageMode languageMode, string variableName)
        {
            SqlParameter[] commandParameters = new SqlParameter[]
            { new SqlParameter("@SurveyId", surveyId), new SqlParameter("@MultiLanguageModeId", languageMode), 
                new SqlParameter("@MultiLanguageVariable", variableName) };
            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spSurveyMultiLanguageModeUpdate", commandParameters);
        }

        /// <summary>
        /// Adds a language to a survey
        /// </summary>
        public void UpdateSurveyLanguage(int surveyId, string languageCode, bool defaultLanguage,string Entity)
        {
            SqlParameter[] commandParameters = new SqlParameter[4];
            commandParameters[0] = new SqlParameter("@SurveyId", SqlDbType.Int, 4);
            commandParameters[0].Value = surveyId;
            commandParameters[1] = new SqlParameter("@LanguageCode", SqlDbType.NVarChar, 50);
            commandParameters[1].Value = languageCode;
            commandParameters[2] = new SqlParameter("@DefaultLanguage", SqlDbType.Bit, 1);
            commandParameters[2].Value = defaultLanguage;
            commandParameters[3] = new SqlParameter("@Entity", SqlDbType.VarChar, 20);
            commandParameters[3].Value = Entity;
            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spMutliLanguageAddForSurvey", commandParameters);
        }

        /// <summary>
        /// Adds a language to a survey
        /// </summary>
        public void AddMultiLanguageText(int languageItemId, string languageCode, int LanguageMessageTypeId ,string ItemText)
        {
            SqlParameter[] commandParameters = new SqlParameter[4];
            commandParameters[0] = new SqlParameter("@LanguageItemId", SqlDbType.Int, 4);
            commandParameters[0].Value = languageItemId;
            commandParameters[1] = new SqlParameter("@LanguageCode", SqlDbType.NVarChar, 50);
            commandParameters[1].Value = languageCode;
            commandParameters[2] = new SqlParameter("@LanguageMessageTypeId", SqlDbType.Int,4);
            commandParameters[2].Value = LanguageMessageTypeId;
            commandParameters[3] = new SqlParameter("@ItemText", SqlDbType.VarChar, 4000);
            commandParameters[3].Value = ItemText;
            SqlHelper.ExecuteNonQuery(SqlHelper.DbConnectionString, CommandType.StoredProcedure, "vts_spMultiLanguageTextAdd", commandParameters);
        }

    }
}

